package com.cacshanghai.cac_oa_client.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cacshanghai.cac_oa_client.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by geyh on 2016/5/27.
 */
public class LeaveActivity extends FragmentActivity {

    private ViewPager viewPager;
    private TextView tv_no_pass, tv_pass;
    private NoPassLeaveFragment noPassLeaveFragment = new NoPassLeaveFragment();
    private PassLeaveFragment passLeaveFragment = new PassLeaveFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave);
        setTitle(getString(R.string.leave_apply) + "(葛仪豪)");
        setBack();
        setTitleRightImageView(R.mipmap.plus);

        tv_no_pass = (TextView) findViewById(R.id.tv_no_pass);
        tv_pass = (TextView) findViewById(R.id.tv_pass);
        viewPager = (ViewPager) findViewById(R.id.vp_leave);
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));
        viewPager.setCurrentItem(0);

        tv_no_pass = (TextView) findViewById(R.id.tv_no_pass);
        tv_pass = (TextView) findViewById(R.id.tv_pass);
        tv_no_pass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);
            }
        });
        tv_pass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);
            }
        });
    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return noPassLeaveFragment;
                case 1:
                    return passLeaveFragment;
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 2;
        }
    }

    public void setTitle(String title) {
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(title);
    }

    public void setBack() {
        ImageView iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    public void setTitleRightImageView(int resId) {
        ImageView iv_right = (ImageView) findViewById(R.id.iv_right);
        iv_right.setVisibility(View.VISIBLE);
        iv_right.setImageResource(resId);
        iv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);
                noPassLeaveFragment.addNewLeave();
            }
        });
    }

}
